package com.example.elastcisearch;

import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class TestGet {

    private RestHighLevelClient client = null;
    
    @Before
    public void before() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.228.104", 9200, "http"));
        client = new RestHighLevelClient(builder);
    }

    /**
     * 
     */
    @SneakyThrows
    @Test
    public void helloworld() {
        GetRequest request = new GetRequest("shop", "001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        if (response.isExists()) {
            System.out.println(response.getId());
            System.out.println(response.getVersion());
            System.out.println(response.getSourceAsString());
        }
    }

    /**
     * 查询包含或不包含字段
     */
    @SneakyThrows
    @Test
    public void sourceInclude() {
        GetRequest request = new GetRequest("shop", "001");
        String[] includes = {"keyword"};
        //String[] excludes = {"desc"};
        FetchSourceContext sourceContext = new FetchSourceContext(true, includes, null);
        request.fetchSourceContext(sourceContext);
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        if (response.isExists()) {
            System.out.println(response.getId());
            System.out.println(response.getVersion());
            System.out.println(response.getSourceAsString());
        }
    }


    /**
     * 异步
     */
    @SneakyThrows
    @Test
    public void sync() {
        GetRequest request = new GetRequest("shop", "001");

        client.getAsync(request, RequestOptions.DEFAULT, new ActionListener<GetResponse>() {
            @Override
            public void onResponse(GetResponse response) {
                if (response.isExists()) {
                    System.out.println(response.getId());
                    System.out.println(response.getVersion());
                    System.out.println(response.getSourceAsString());
                }
                
            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });
        

        TimeUnit.SECONDS.sleep(1000);
    }
    
    
    
    
}
